package cate.game.rank.po;

import cate.game.role.Role;
import cate.game.role.RoleSnapshot;
import cp.solution.pattern.ICopiable;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.Getter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

@NoteClass(value = "排行榜数据")
@ToString(callSuper = true)
public class Rank implements ICopiable<Rank> {
    @NoteField(value = "该条记录所归属的主角de快照")
    @Getter
    public RoleSnapshot owner;

    @NoteField(value = "记录上榜时间")
    @Getter
    public long recordTime;

    @NoteField(value = "上榜的值")
    @Getter
    public long value;

    @NoteField(value = "额外的上榜值")
    @Getter
    public long extraValue;

    @NoteField(value = "点赞数")
    @Getter
    public long admireNum;

    @NoteField(value = "录像唯一id")
    public String replayUid;

    public Rank(){

    }

    public Rank(Role role, long value){
        this(role,value,0);
    }

    public Rank(Role role,long value, long extraValue){
        this(role, value, extraValue, null);
    }

    public Rank(Role role, long value, String replayUid){
        this(role, value, 0, replayUid);
    }

    public Rank(Role role, long value, long extraValue, String replayUid){
        this(new RoleSnapshot(role), value, extraValue, replayUid);
    }

    public Rank(RoleSnapshot roleSnapshot, long value){
        this(roleSnapshot, value, 0, null);
    }

    public Rank(RoleSnapshot roleSnapshot, long value, long extraValue){
        this(roleSnapshot, value, extraValue, null);
    }

    public Rank(RoleSnapshot roleSnapshot, long value, String replayUid){
        this(roleSnapshot, value, 0, replayUid);
    }

    public Rank(RoleSnapshot roleSnapshot, long value, long extraValue, String replayUid){
        this.owner = roleSnapshot.copy();
        this.value = value;
        this.recordTime = System.currentTimeMillis();
        this.extraValue = extraValue;
        this.replayUid = replayUid;
    }

    public void updateOwner(Role role){
        updateOwner(role, 0);
    }

    public void updateOwner(Role role, long newValue){
        updateOwner(role, newValue, 0);
    }

    public void updateOwner(Role role, long newValue, long extraValue){
        updateOwner(role, newValue, extraValue, null);
    }

    public void updateOwner(Role role, long newValue, long extraValue, String replayUid){
        if(owner != null){
            owner.read(role);
        }
        if(newValue > 0 && newValue != value){
            value = newValue;
            //数据一样就不更新上榜时间了，为了后续做排序
            recordTime = System.currentTimeMillis();
        }
        if(extraValue > 0){
            this.extraValue = extraValue;
        }
        if(StringUtils.isNotBlank(replayUid)){
            this.replayUid = replayUid;
        }
    }

    public void updateOwner(RoleSnapshot roleSnapshot, long newValue){
        updateOwner(roleSnapshot,newValue,0);
    }

    public void updateOwner(RoleSnapshot roleSnapshot, long newValue, long extraValue){
        updateOwner(roleSnapshot,newValue,extraValue,null);
    }

    public void updateOwner(RoleSnapshot roleSnapshot, long newValue, long extraValue, String replayUid){
        if(roleSnapshot != null){
            owner = roleSnapshot.copy();
        }
        if(newValue > 0 && newValue != value){
            value = newValue;
            //数据一样就不更新上榜时间了，为了后续做排序
            recordTime = System.currentTimeMillis();
        }
        if(extraValue > 0){
            this.extraValue = extraValue;
        }
        if(StringUtils.isNotBlank(replayUid)){
            this.replayUid = replayUid;
        }
    }

    @Override
    public Rank copy() {
        Rank rank = new Rank();
        rank.owner = this.owner.copy();
        rank.recordTime = this.recordTime;
        rank.value = this.value;
        rank.admireNum = this.admireNum;
        rank.extraValue = this.extraValue;
        return rank;
    }
}
